<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

ini_set('max_execution_time', 3600);
ini_set('memory_limit', '128M');

function wb_connect() {
	$dbhost = '72.3.204.165';
	$dbuser = '549152_whiteboar';
	$dbpass = 'ZGY6AWWGheBEZRpsZDsk';
	$dbname = '549152_whiteboardg';
	
	// TODO: return -1 on error?
	$conn = @mysql_connect($dbhost, $dbuser, $dbpass);
	if (!$conn) {
		die('error code 157. failed connecting to database');
	}
	mysql_select_db($dbname);
	return $conn;
}

function run_query($query, $link) {
	//echo 'QUERY = ' . $query . '<br>';
	$result = mysql_query($query, $link);
	if (!$result) {
		//echo 'Could not run query: '.$query."<br>\n";
		echo 'MySQL error: '.mysql_error();
		exit;
	}
	return $result;
}

$offset = 10000 * get_input('wpage');

$link = wb_connect();

$query = "SELECT d.id, d.comment, e.userid, d.model, d.deviceuid, d.location, d.md5, d.url, d.thumbnail_url FROM drawings d, elgg_artists e WHERE d.id IN (SELECT drawingid FROM elgg_artists) d.id = e.drawingid";
echo $query;

$result = run_query($query, $link);

while($row = mysql_fetch_assoc($result)) {
    $id                     = $row['id'];
    $title                  = $row['comment'];
    $description            = $row['comment'];
    $user_id                = $row['userid'];
    $shared                 = 0;
    $width                  = 640;
    $height                 = 960;
    $orientation            = 1;
    $device_model           = $row['model'];
    $device_uid             = $row['deviceuid'];
    $location = $row['location'];
    // <+50.46113598, +13.40047019> +/- 1494.00m (speed -1.00 mps / course -1.00) @ 2009-09-12 13:23:54 +0200
    if(strlen($location) > 15) {
        $s = strpos($location, "<");
        $e = strpos($location, ">");
        if($s >= 0 && $e > $s) {
            $location = trim(substr($location, $s + 1, $e - $s - 1));
            $larray = explode(',', $location);
            foreach($larray as $key => $value) {
                    $larray[$key] = trim($larray[$key]);
            }
        }
        $location = "$larray[0],$larray[1]";
        $lat      = $larray[0];
        $lng      = $larray[1];
    } else {
        $lat      = 0;
        $lng      = 0;
    }
    $location_description   = '';
    $md5                    = $row['md5'];
    $url                    = $row['url'];
    $thumbnail_url          = $row['thumbnail_url'];
    if($thumbnail_url) {
        
    } else {
        //$thumbnail_url = str_replace("rackcdn.com/", "rackcdn.com/thumb_", $url);
        $thumbnail_url = $url;
    }
    $timestamp              = strtotime($row['date']);
    $drawing_id = port_drawing($id, $title, $description, $user_id, $shared, 
                        $width, $height, $orientation, 
                        $device_model, $device_uid, 
                        $md5, $url, $thumbnail_url, 
                        $lat, $lng, $location_description, $timestamp);
    echo $drawing_id . '<br>';
}

?>
